package com.qf.cardlike.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.cardlike.domain.Card;
import com.qf.cardlike.mapper.CardMapper;
import com.qf.cardlike.service.CardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class CardServiceImpl implements CardService {
    @Autowired
    CardMapper cardMapper;
    @Override
    public void addCard(Card card) {
        cardMapper.insertSelective(card);
    }

    @Override
    public PageInfo<Card> selectCardByPage(Integer pageIndex, Integer pageSize) {
        PageHelper.startPage(pageIndex,pageSize);
        List<Card> cards = cardMapper.selectAll();
        return new PageInfo<>(cards);
    }

    @Override
    public Card selectById(Integer id) {
        Card card = cardMapper.selectByPrimaryKey(Long.valueOf(id));
        card.setVisited(card.getVisited()+1);
        cardMapper.updateByPrimaryKeySelective(card);
        return card;
    }

    @Override
    public PageInfo<Card> selectByIds(Integer pageIndex, Integer pageSize, String likeIds) {
        List<String> idList = Arrays.asList(likeIds.split(","));
        PageHelper.startPage(pageIndex,pageSize);
        List<Card> cards = cardMapper.selectByIds(idList);
        return new PageInfo<>(cards);
    }
}
